1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::privs::*;
use crate::prelude::*;
use crate::version::ffi;
impl_handle! { HVERSIONINFO;
/// Handle to a
/// [version info](https://learn.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfow)
/// block.
///
/// Originally just a pointer to a memory block.
}
impl version_Hversioninfo for HVERSIONINFO {}
/// This trait is enabled with the `version` feature, and provides methods for
/// [`HVERSIONINFO`](crate::HVERSIONINFO).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait version_Hversioninfo: Handle {
/// [`GetFileVersionInfo`](https://learn.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfow)
/// function.
///
/// The returned buffer will be automatically allocated with
/// [`HVERSIONINFO::GetFileVersionInfoSize`](crate::prelude::version_Hversioninfo::GetFileVersionInfoSize).
#[must_use]
fn GetFileVersionInfo(file_name: &str) -> SysResult<VersionInfoGuard> {
let block_sz = Self::GetFileVersionInfoSize(file_name)?;
let mut hglobal = HGLOBAL::GlobalAlloc(
Some(co::GMEM::FIXED | co::GMEM::ZEROINIT),
block_sz as _,
)?;
let hglobal_ptr = hglobal.leak();
bool_to_sysresult(
unsafe {
ffi::GetFileVersionInfoW(
WString::from_str(file_name).as_ptr(),
0,
block_sz,
hglobal_ptr.ptr(),
)
},
).map(|_| unsafe {
VersionInfoGuard::new(
HVERSIONINFO::from_ptr(hglobal_ptr.ptr()), // simply use the HGLOBAL pointer
)
})
}
/// [`GetFileVersionInfoSize`](https://learn.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfosizew)
/// function.
///
/// You don't need to call this function directly, because
/// [`HVERSIONINFO::GetFileVersionInfo`](crate::prelude::version_Hversioninfo::GetFileVersionInfo)
/// already calls it.
#[must_use]
fn GetFileVersionInfoSize(file_name: &str) -> SysResult<u32> {
let mut dw_handle = u32::default();
match unsafe {
ffi::GetFileVersionInfoSizeW(
WString::from_str(file_name).as_ptr(),
&mut dw_handle,
)
} {
0 => Err(GetLastError()),
sz => Ok(sz)
}
}
/// Calls
/// [`HVERSIONINFO::VarQueryValue`](crate::prelude::version_Hversioninfo::VarQueryValue)
/// to retrieve a reference to a slice with all languages and code pages.
///
/// # Examples
///
/// Listing all pairs of language and code page:
///
/// ```no_run
/// use winsafe::{self as w, prelude::*};
///
/// let exe_name = w::HINSTANCE::NULL.GetModuleFileName()?;
/// let hversion = w::HVERSIONINFO::GetFileVersionInfo(&exe_name)?;
///
/// for (lang, cp) in hversion.langs_and_cps()?.iter() {
/// println!("{} {}", lang, cp);
/// }
/// # w::SysResult::Ok(())
/// ```
#[must_use]
fn langs_and_cps(&self) -> SysResult<&[(LANGID, co::CP)]> {
unsafe {
self.VarQueryValue::<(LANGID, co::CP)>("\\VarFileInfo\\Translation")
.map(|(pblocks, sz)|
std::slice::from_raw_parts(
pblocks,
sz as usize / std::mem::size_of::<(LANGID, co::CP)>(),
)
)
}
}
/// [`VarQueryValue`](https://learn.microsoft.com/en-us/windows/win32/api/winver/nf-winver-verqueryvaluew)
/// function.
///
/// # Safety
///
/// The returned pointer and size vary according to `lpSubBlock`. If you set
/// it wrong, you're likely to cause a buffer overrun.
///
/// This function is rather tricky, consider using the high-level methods:
/// * [`langs_and_cps`](crate::prelude::version_Hversioninfo::langs_and_cps);
/// * [`str_val`](crate::prelude::version_Hversioninfo::str_val);
/// * [`version_info`](crate::prelude::version_Hversioninfo::version_info).
///
/// # Examples
///
/// Reading version information from resource:
///
/// ```no_run
/// use winsafe::{self as w, prelude::*};
///
/// let exe_name = w::HINSTANCE::NULL.GetModuleFileName()?;
/// let hversion = w::HVERSIONINFO::GetFileVersionInfo(&exe_name)?;
///
/// let (pvsf, sz_data) = unsafe {
/// hversion.VarQueryValue::<w::VS_FIXEDFILEINFO>("\\")?
/// };
///
/// let ver = unsafe { &*pvsf }.dwFileVersion();
/// println!("Version {}.{}.{}.{}",
/// ver[0], ver[1], ver[2], ver[3]);
/// # w::SysResult::Ok(())
/// ```
#[must_use]
unsafe fn VarQueryValue<T>(&self,
sub_block: &str,
) -> SysResult<(*const T, u32)>
{
let mut lp_lp_buffer = std::ptr::null();
let mut pu_len = 0;
bool_to_sysresult(
ffi::VerQueryValueW(
self.ptr(),
WString::from_str(sub_block).as_ptr(),
&mut lp_lp_buffer as *mut _ as _,
&mut pu_len,
),
).map(|_| (lp_lp_buffer as *const T, pu_len))
}
/// Calls
/// [`HVERSIONINFO::VarQueryValue`](crate::prelude::version_Hversioninfo::VarQueryValue)
/// to retrieve a string value.
///
/// Common value names are:
/// * Comments
/// * CompanyName
/// * FileDescription
/// * FileVersion
/// * InternalName
/// * LegalCopyright
/// * LegalTrademarks
/// * OriginalFilename
/// * ProductName
/// * ProductVersion
/// * PrivateBuild
/// * SpecialBuild
///
/// # Examples
///
/// Reading product name and legal copyright from resource:
///
/// ```no_run
/// use winsafe::{self as w, prelude::*};
///
/// let exe_name = w::HINSTANCE::NULL.GetModuleFileName()?;
/// let hversion = w::HVERSIONINFO::GetFileVersionInfo(&exe_name)?;
///
/// let (lang0, cp0) = hversion.langs_and_cps()?[0]; // first language and code page
///
/// println!(
/// "{}\n{}",
/// hversion.str_val(lang0, cp0, "ProductName")?,
/// hversion.str_val(lang0, cp0, "LegalCopyright")?,
/// );
/// # w::SysResult::Ok(())
/// ```
#[must_use]
fn str_val(&self,
lang_id: LANGID,
code_page: co::CP,
name: &str,
) -> SysResult<String> {
unsafe {
self.VarQueryValue::<u16>(
&format!("\\StringFileInfo\\{:04x}{:04x}\\{}",
u16::from(lang_id), u16::from(code_page), name),
).map(|(pstr, len)|
WString::from_wchars_slice(
std::slice::from_raw_parts(pstr, len as _),
).to_string()
)
}
}
/// Calls
/// [`HVERSIONINFO::VarQueryValue`](crate::prelude::version_Hversioninfo::VarQueryValue)
/// to retrieve a reference to the fixed version block, if any.
#[must_use]
fn version_info(&self) -> SysResult<&VS_FIXEDFILEINFO> {
unsafe {
self.VarQueryValue::<VS_FIXEDFILEINFO>("\\")
.map(|(p, _)| &*p)
}
}
}